package es.efor.ics.gev.model;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;
import javax.persistence.Version;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.joda.time.Hours;

/**
 * The persistent class for the <b>MODULOS</b> database table.
 * 
 * @author dlisbona
 *
 */
@Entity
@Table(name = "MODULOS")
public class Modulo extends BaseObject implements Serializable {

	// static data
	private static final long serialVersionUID = 8197102271218932328L;

	// private instance data
	private Long id;	  
	private String nomenclatura;
	private Hours horas;
	private Programa programa;
	private Integer version;
	
	/**
	 * Construct an uninitialized <code>Modulo</code> (for runtime proxy
	 * generation in Hibernate).
	 */
	protected Modulo() {		
	}
	
	/**
	 * Construct an initialized <code>Modulo</code>.
	 * 
	 * @param nomenclatura
	 * @param horas
	 */
	public Modulo(String nomenclatura, Hours horas) {
		this.nomenclatura = nomenclatura;
		this.horas = horas;
	}

	// ********************** Accessor Methods ********************** //
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column(name = "MODULO_ID")
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Column(name = "NOMENCLATURA_MODULO", length = 255, nullable = false)
	public String getNomenclatura() {
		return nomenclatura;
	}

	public void setNomenclatura(String nomenclatura) {
		this.nomenclatura = nomenclatura;
	}

	@Column(name = "HORAS")
	@org.hibernate.annotations.Type(type="org.joda.time.contrib.hibernate.PersistentPeriod")
	public Hours getHoras() {
		return horas;
	}
	
	public void setHoras(Hours horas) {
		this.horas = horas;
	}

	@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@PrimaryKeyJoinColumn
	@JoinColumn(referencedColumnName = "PROGRAMA_MODULO_ID", name = "MODULO_ID")
	@org.hibernate.annotations.LazyToOne(value = org.hibernate.annotations.LazyToOneOption.PROXY)
	public Programa getPrograma() {
		return programa;
	}

	public void setPrograma(Programa programa) {
		this.programa = programa;
	}

	@Version
	@Column(name = "OBJ_VERSION")
	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}
	
	// ********************** Common Methods ********************** //
	
	@Override
	public boolean equals(Object obj) {
		if (obj instanceof RevisionDocumento == false) {
			return false;
		}
		if (this == obj) {
			return true;
		}
		Modulo rhs = (Modulo) obj;
		return new EqualsBuilder().
			append(this.getNomenclatura(), rhs.getNomenclatura()).
				isEquals();
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder(17, 37).
		append(this.getNomenclatura()).
		toHashCode();
	}

	@Override
	public String toString() {
		return new ToStringBuilder(this, ToStringStyle.DEFAULT_STYLE).
		append("nomenclatura", this.getNomenclatura()).toString();	
	}

}
